using System;
using UnityEngine;
using UnityEditor;

namespace GameDesigner
{
    [InitializeOnLoad]
    internal class LayerSettingsWindow : EditorWindow
    {
        private static Styles s_Styles;
        private StateLayer layer;
        public static LayerSettingsWindow layerSettingsWindow;
        private static long lastClosedTime;

        private Vector2 windowSize => new(270f, EditorGUIUtility.singleLineHeight * 7f);

        private void OnEnable()
        {
        }

        private void OnDisable()
        {
            layerSettingsWindow = null;
            lastClosedTime = DateTime.Now.Ticks / 10000L;
        }

        internal static bool ShowAtPosition(Rect buttonRect, StateLayer layer)
        {
            if (DateTime.Now.Ticks / 10000L < lastClosedTime + 50L)
                return false;
            Event.current.Use();
            if (layerSettingsWindow == null)
                layerSettingsWindow = CreateInstance<LayerSettingsWindow>();
            layerSettingsWindow.layer = layer;
            layerSettingsWindow.Init(buttonRect);
            return true;
        }

        private void Init(Rect buttonRect)
        {
            buttonRect = GUIUtility.GUIToScreenRect(buttonRect);
            ShowAsDropDown(buttonRect, windowSize);
            Focus();
            wantsMouseMove = true;
        }

        internal void OnGUI()
        {
            if (s_Styles == null)
                s_Styles = new Styles();
            if (layer == null)
                return;
            GUI.Box(new Rect(0.0f, 0.0f, position.width, position.height), GUIContent.none, "grey_border");
            EditorGUIUtility.labelWidth = 100f;
            EditorGUI.BeginChangeCheck();
            layer.Weight = EditorGUILayout.Slider(s_Styles.weight, layer.Weight, 0.0f, 1f);
            layer.AvatarMask = EditorGUILayout.ObjectField(s_Styles.mask, layer.AvatarMask, typeof(AvatarMask), false) as AvatarMask;
            layer.IKPass = EditorGUILayout.Toggle(s_Styles.ik, layer.IKPass);
            if (EditorGUI.EndChangeCheck())
            {
                layer.SetLayerMask(layer.AvatarMask);
                EditorUtility.SetDirty(StateMachineWindow.support);
            }
        }

        private class Styles
        {
            public readonly GUIContent weight = EditorGUIUtility.TrTextContent("Weight", "Change layer default weight.");
            public readonly GUIContent blending = EditorGUIUtility.TrTextContent("Blending", "Choose between Override and Additive layer.");
            public readonly GUIContent sync = EditorGUIUtility.TrTextContent("Sync", "Synchronize this layer with another layer.");
            public readonly GUIContent timing = EditorGUIUtility.TrTextContent("Timing", "When active, the layer will take control of the duration of the Synced Layer.");
            public readonly GUIContent ik = EditorGUIUtility.TrTextContent("IK Pass", "When active, the layer will have an IK pass when evaluated. It will trigger an OnAnimatorIK callback.");
            public readonly GUIContent sourceLayer = EditorGUIUtility.TrTextContent("Source Layer", "Specifies the source of the Synced Layer.");
            public readonly GUIContent mask = EditorGUIUtility.TrTextContent("Mask", "The AvatarMask that is used to mask the animation on the given layer.");
        }
    }
}